/**
  * @authors Tyler Williams, Cameron Stewart, Fatimah Alowa, Marlon Boothe, Lauren Walker
  * @version group2 project 2 part 2
  * @since 2012-09-15
  */

#include "common.h"

/**
 * main function gets the name of the source file from the command line
 * and then calls the functions init_lister() and get_source_line()
 *
 * @param argc           int variable
 * @param *arg[]        character pointer
 *
 * @return 0
 */
 
int main(int argc, char *argv[]){
	FILE *file=NULL;
	char source_file[MAX_FILE_NAME_LENGTH];
	char date[DATE_STRING_LENGTH]={'\0'};
	file=init_lister(argv[1], source_file, date);
	while(get_source_line(file,argv[1],date));
	return 0;
}

/**
 *  *init_lister function receives the file name
 *    and sets the date and time in the file.
 *
 * @param *name                 constant character pointer  
 * @param source_file_name[]    character array
 * @param  dte[]                character array
 * 
 * @return file variable
 */
 
FILE *init_lister(const char *name, char source_file_name[], char dte[]){ 
	time_t t;
	t = time(NULL); 
	FILE *file=NULL;
	strcpy(source_file_name, name);
	file=fopen(source_file_name, "r+");
	strcpy(dte,asctime(localtime(&t)));
	return file;
}

/**
 * get_source_line function repeatedly to read and print source lines
 *
 * @param *src_file              FILE Pointer
 * @param src_name[]            character array
 * @param todays_date[]           character array
 *
 * @return      true if it successfully reads and prints a line, returns false if it reached the end of the source file
*/

BOOLEAN get_source_line(FILE *src_file, char src_name[], char todays_date[]){
	char print_buffer[MAX_PRINT_LINE_LENGTH]={'\0'}; 
	char source_buffer[MAX_SOURCE_LINE_LENGTH]={'\0'};
	static int line_number=0;

	if(fgets(source_buffer, sizeof source_buffer, src_file)!='\0'){
		line_number=line_number+1;
		sprintf(print_buffer, "%d - %s", line_number, source_buffer);
		print_line(print_buffer, src_name, todays_date);
		return TRUE;
	}else{
		return FALSE;
	}
}